package uk.ac.glasgow.demosocs.impl.ui;

import uk.ac.glasgow.clui.ArgumentsDescription;
import uk.ac.glasgow.clui.CommandDescription;
import uk.ac.glasgow.clui.MinimumArguments;
import uk.ac.glasgow.clui.SystemCommand;
import uk.ac.glasgow.clui.SystemCommandException;
import uk.ac.glasgow.demosocs.Election;
import uk.ac.glasgow.demosocs.ElectionState;
import uk.ac.glasgow.demosocs.VotingSystem;
import uk.ac.glasgow.demosocs.impl.CandidateImpl;
import uk.ac.glasgow.demosocs.impl.ElectionImpl;

@MinimumArguments(1)
@CommandDescription("Nominate the specified candidate in the current election.")
@ArgumentsDescription("<candidate>")
public class NominateCandidateCommand extends SystemCommand<VotingSystem> {

	public NominateCandidateCommand(VotingSystem facade) {
		super(facade);
	}

	@Override
	public String processCommand(String[] arguments)
			throws SystemCommandException {
		
		String guid = facade.getCurrentUserGUID();
		if (guid == null)
			throw new SystemCommandException("No current user set.");
		
		Election election = facade.getCurrentElection();
		
		if (election == null)
			throw new SystemCommandException("No current election set.");
		
		if (!election.getElectionState().equals(ElectionState.NOMINATIONS_OPEN) && 
					!election.getElectionState().equals(ElectionState.STARTED) )
			throw new SystemCommandException("Nominations are not open");
		
		String candidate = arguments[0];
		
		facade.nominateCandidate(candidate);
		
		if (election.getCandidates().contains(new CandidateImpl(candidate)))
			return "This candidate has already been nominated for this election";
		
		
		if (!((ElectionImpl) election).getUnapprovedCandidates().contains(new CandidateImpl(candidate)))
			return "Nomination not successful.";		
		
		
		return "A nomination of candidate ["+candidate+
		"] for election ["+election.getEID()+
		"] was submitted by ["+facade.getCurrentUserGUID()+"].";
	}

}
